#if !defined(DIFFUSION_DOWNHILLSIMPLEX_H)
#define DIFFUSION_DOWNHILLSIMPLEX_H


#include "nr.h"

class DownhillSimplex
{
public:
  DownhillSimplex(int NParams) 
  {
    newA(ptry,NParams);

    newM(Corners,NParams+1,NParams);
    newA(CornerValues,NParams+1);

    newA(psum,NParams);
  }

  ~DownhillSimplex()
  {
    deleteA(ptry);

    deleteM(Corners);
    deleteA(CornerValues);

    deleteA(psum);
  }

  double Eval(double *I, double &FuncVal, int ndim, const double ftol, 
              const double StartSize, double (*funk)(double []));
private:
  void amoeba(double **p, double y[], int ndim, const double ftol,
              double (*funk)(double []));

  double amotry(double **p, double y[], double psum[], int ndim,
             double (*funk)(double []), int ihi, double fac);


  // used in amotry
  double *ptry; // holds new trial simplex point obtained by reflection
                // through line given by p_high and centriod point of
                // all remainder points in simplex

  // used in Eval

  double **Corners;  // ndim+1 corners to obtain simplex
  double *CornerValues; // function values at each of these corners

  // used in amoeba

    double *psum;   // holds sum of all simplex points 
};



#endif // !defined(DIFFUSION_DOWNHILLSIMPLEX_H)
